<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%@ include file="/common/huanxin.jsp"%>
<%
	User me = userByUid(token.getUid());
	if (me == null || !Tools.getDesc(me.getJson("role"), "1").equals("3")) {
		Tools.encryptPrint(out, new Message(30031, "您无权限进行该操作"));
		return;
	}
	Integer deptId = me.getDeptId();
	if (deptId == null) {
		Tools.encryptPrint(out, new Message(30041, "不存在该科室"));
		return;
	}
	String name = input.getDesc("name", "");
	String phone = input.getDesc("phone", "");
	Integer role = input.getInt("role", 1);
	String password = input.getDesc("password", "");
	Integer gender = input.getInt("gender", 1);
	String employeeId = input.getDesc("employee_id", "");
	String title  = input.getDesc("title", "");
	String position = input.getDesc("position", "");
	String teachTitle = input.getDesc("teach_title", "");
	List<User> list = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("u.employeeId=", employeeId).and("u.type=", USER_EMPLOYEE)).page(1, 1).query().getResultObj();
	if (!isEmpty(list)) {
		Tools.encryptPrint(out, new Message(30031, "该工号已被绑定"));
		return ;
	}
	if (isEmpty(name)) {
		Tools.encryptPrint(out, new Message(30041, "姓名不能为空"));
		return ;
	}
	String regexStr = "^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$";
	if (!name.matches(regexStr)) {
		msg = new Message(30041, "姓名只能为中文汉字");
		return;
	}
	if (name.length() > 10 || name.length() < 2 ) {
		msg = new Message(30041, "姓名为2-10位汉字");
		Tools.encryptPrint(out, msg);
		return;
	}
	if (!isChinaPhoneLegal(phone)) {
		Tools.encryptPrint(out, new Message(30041, "手机号不合法"));
		return ;
	}
	if (isEmpty(password)) {
		Tools.encryptPrint(out, new Message(30041, "密码不能为空"));
		return ;
	}
	list = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("(u.userName = '" + phone + "' or u.phone='" + phone + "')").and("u.type=", USER_EMPLOYEE)).page(1, 1).query().getResultObj();
	if (!isEmpty(list)) {
		Tools.encryptPrint(out, new Message(30031, "该手机号已经绑定了其他账户"));
		return ;
	}
	User user = new User();
	user.setDeptId(deptId);
	user.setGender(gender);
	user.setJson("role", role + "");
	user.setName(name);
	user.setPhone(phone);
	user.setUserName(generateUserName(phone));
	user.setPassword(password);
	user.setRepassword(password);
	user.setHospital(hospital);
	user.setType(USER_EMPLOYEE);
	user.setTitle(filterAll(title));
	user.setPhone(phone);
	user.setJson("position", filterAll(position));
	user.setJson("teach_title", filterAll(teachTitle));
	Integer uid = (Integer)DBTools.db("basedb").addItem(user);
	register(uid + "", "123456", "eeesys-txby/deptcommon", hospital);
	Tools.encryptPrint(out, msg);
%>
